package cn.ffcs.uec.organization.component;

import java.util.Collection;
import java.util.List;

import lombok.Getter;
import lombok.Setter;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zul.Tree;
import org.zkoss.zul.Treecell;
import org.zkoss.zul.Treeitem;

import cn.ffcs.uec.common.util.PermissionUtil;
import cn.ffcs.uec.organization.vo.OrganizationRelationVo;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.zkplus.zul.tree.model.BaseTreeModel;
import cn.ffcs.uom.common.zkplus.zul.tree.node.impl.TreeNodeImpl;
import cn.ffcs.uom.common.zkplus.zul.tree.render.BaseTreeitemRenderer;
@Controller
@Scope("prototype")
public class UecOrganizationRelationTree extends Tree implements IdSpace {

	private static final long serialVersionUID = 7653806512579163823L;
	/**
	 * 指定的根组织父id
	 */
	@Getter
	@Setter
	private String rootId;
	/**
	 * 构造函数.
	 */
	public UecOrganizationRelationTree() {
		Components.wireVariables(this, this);
		Components.addForwards(this, this, '$');
	}

	/**
	 * 创建
	 */
	public void onCreate() {
		this.setSclass("zt-tree-scroll");
		bindTree();
	}

	/**
	 * 绑定树
	 */
	@SuppressWarnings("deprecation")
	public void bindTree() {
		OrganizationRelationVo orgRelVo = new OrganizationRelationVo();
		orgRelVo.setIsRoot(true);
		// 如果前台没有设置根节点值，则根据登录的用户获取根节点
		
		if(StrUtil.isNullOrEmpty(rootId)) {
		// 如果在配置表里面为区本部，则能够看到所有组织
		// 否则获取当前用户组织
			String code = PlatformUtil.getCurrentUser().getScreenName().toUpperCase();
		if("区本部".equals(PermissionUtil.getManagerOrg()) || code.startsWith("W2019")) {
			rootId = "0001A11000000003UHTY";
		} else {
			rootId = PermissionUtil.getFullOrg();
		}
		
	}
	orgRelVo.setRootId(rootId);
		final TreeNodeImpl<OrganizationRelationVo> treeNode = new TreeNodeImpl<OrganizationRelationVo>(
				orgRelVo);
		treeNode.readChildren();																																									
		this.setModel(new BaseTreeModel(treeNode));
		this.setTreeitemRenderer(new BaseTreeitemRenderer());
		this.setLableStyle();
	
		/*if(StrUtil.isNullOrEmpty(rootId)) {
			// 如果在配置表里面为区本部，则能够看到所有组织
			// 否则获取当前用户组织
			
			String managerName=PermissionUtil.getManagerOrg();
			String[] split = managerName.split(",");
			if(split.length==1) {
				if("区本部".equals(split[0])) {
					rootId = "0001A11000000003UHTY";
				}else {
					rootId = PermissionUtil.getFullOrg();
				}
				orgRelVo.setRootId(rootId);
				final TreeNodeImpl<OrganizationRelationVo> treeNode = new TreeNodeImpl<OrganizationRelationVo>(
						orgRelVo);
				treeNode.readChildren();																																									
				this.setModel(new BaseTreeModel(treeNode));
				this.setTreeitemRenderer(new BaseTreeitemRenderer());
				this.setLableStyle();	
			}
		else {
			rootId="";
			for (int i = 0; i < split.length; i++) {
				rootId=	PermissionUtil.getOrgRootId(split[i]);
			
				orgRelVo.setRootId(rootId);
				final TreeNodeImpl<OrganizationRelationVo> treeNode = new TreeNodeImpl<OrganizationRelationVo>(
						orgRelVo);
				treeNode.readChildren();																																									
				this.setModel(new BaseTreeModel(treeNode));
				this.setTreeitemRenderer(new BaseTreeitemRenderer());
				this.setLableStyle();
			 }	
			}
		}*/
	}
	
	@SuppressWarnings({"unchecked", "rawtypes"})
	public void setLableStyle() {
		Collection<Treeitem> tis = this.getItems();
		for (Treeitem ti : tis) {
			OrganizationRelationVo organizationRelationVo = (OrganizationRelationVo) ((TreeNodeImpl) ti
					.getValue()).getEntity();
			if (organizationRelationVo != null) {
				Treecell cell = null;
				List<Object> list = ti.getTreerow().getChildren();
				if (list != null) {
					cell = (Treecell) list.get(0);
				}
				if("ORG".equals(organizationRelationVo.getOrgType())){
					if (cell != null) {
						cell.setStyle("color:blue");
					}
				}
			}
		}
	}

}
